'''
实现图结构的序列化存储和加载
'''
import networkx as nx
import json
import os
import pickle
def graph_init(node,edge):
    '''根据社交网络节点和边的信息绘制社交网络图'''
    id=list(node['numeric_id'])
    cols=list(node)
    G=nx.Graph()
    G.add_nodes_from(id)
    for i in id:
        for k in range(1,len(cols)):
            G.nodes[i][cols[k]]=node.loc[i,cols[k]]
    G.add_edges_from(edge)
    return G
def save(graph):
    '''序列化图信息'''
    file=pickle.dumps(graph)#二进制存储
    return file
def load_graph(file):
    '''将网络加载至内存'''
    road='/Users/wll/Documents/现代/' #判断该路径的目录是否存在
    if not os.path.exists(road):
        os.mkdir(road) #若不存在则创造
    fb= open(road+'graph.json',mode='wb')
    fb.write(file)
    print('文件创建成功')
